package com.gxa.hualian.controller;

import com.gxa.hualian.common.JsonResult;
import com.gxa.hualian.pojo.User;
import com.gxa.hualian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author bill
 * @date 2021/10/21 10:38
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService = null;

    @PostMapping("/get")
    public Map get(@RequestParam("id") Long id){
        User user = userService.getById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(user);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getList")
    public Map getList(@RequestParam("name") String name,
                       @RequestParam("page")Integer page,
                       @RequestParam("size")Integer size){

        System.out.println("name:"+name);
        System.out.println("page:"+page);
        System.out.println("size:"+size);

        return null;
    }

    @PostMapping("/insert")
    public void insert(User user){
        System.out.println(user);
    }

    @PostMapping("/update")
    public void update(@RequestBody User user){
        System.out.println(user);
    }

    @PostMapping("/getCpnUsedList")
    public Map getCpnUsedList(@RequestBody Map map){
        System.out.println(map);
        System.out.println("name:"+map.get("name"));

        JsonResult jsonResult = new JsonResult();
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    public Map delete(@RequestHeader("token") String token){
        System.out.println("token:"+token);
        userService.deleteById(1L);
        return null;
    }

    @PostMapping("/login")
    public Map login(HttpSession session){
        // 登录判断过程
        // 判断通过,生成token
        //  把token记录在session
        session.setAttribute("token","token123");
        // 返回token给前端
        return null;
    }

    @PostMapping("/index")
    public Map index(HttpSession session){
        // 在session取出token
        System.out.println("session:"+session.getAttribute("token"));
        // 返回token给前端

        return null;
    }

}
